import sys
from tkinter import messagebox
from PySide6.QtWidgets import *

go_func = ""
add_file_func = ""
filelist_list = []

def dependency():
    return "ftlib"

def get_filelist():
    for i in filelist_list:
        print(i)

def need():
    return "add_file,go,filelist,FTlib"

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("PySide6 GUI")
        
        # 创建一个中央小部件
        central_widget = QWidget(self)
        self.setCentralWidget(central_widget)

        # 创建一个布局
        layout = QVBoxLayout(central_widget)
        GetUrlText = QLineEdit()
        layout.addWidget(GetUrlText)
        AddFilesButton = QPushButton("添加文件", clicked=lambda: add_file_func(GetUrlText.text()))
        layout.addWidget(AddFilesButton)
        GoButton = QPushButton("开始下载", clicked=lambda: go_func())
        layout.addWidget(GoButton)
        FileListButton = QPushButton("查看文件列表", clicked=lambda: get_filelist())
        layout.addWidget(FileListButton)
        RunCommandText = QLineEdit()
        layout.addWidget(RunCommandText)
        if ftapi_enabled:
            CommandPromptButton = QPushButton("运行CMD命令（基于FTlib）", clicked=lambda: ftapi.cmd(RunCommandText.text()))
            layout.addWidget(CommandPromptButton)

ftapi = None
ftapi_enabled = True
try:
    def run(add_file, go, filelist, FTlib):
        global ftapi
        try:
            ftapi = FTlib("PySide6 GUI")
            ftapi.cmd("dir")
        except:
            messagebox.showerror("错误","无FTlib库")
            ftapi_enabled = False
        global add_file_func, go_func, filelist_list
        add_file_func = add_file
        go_func = go
        filelist_list = filelist
        app = QApplication(sys.argv)

        window = MainWindow()
        window.resize(400, 300)
        window.show()

        sys.exit(app.exec())
except:
    messagebox.showerror("错误","FTlib Not Found")

def description():
    return "LDownload PySide6 GUI 插件"

if __name__ == '__main__':
    print("这是个LDownload插件，请在LDownload主程序中运行")